package com.llc.uh.user;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class CreateUserDataStore extends HttpServlet
{

    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
    {

        Key userKey;
        Date date = new Date();
        /**
         * Deletes the users in the datastore.
         */
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

        Query query = new Query("User");
        List<Entity> users = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(100));

        for (Entity user : users)
        {
            userKey = user.getKey();
            datastore.delete(userKey);
        }

        /**
         * add one user, later add zero...
         */

        Entity user = new Entity("User");

        user.setProperty("email", "llc.tester@hawaii.edu");
        user.setProperty("quizname", "Screening Exam for Chn399");
        user.setProperty("rawscore", 10);
        user.setProperty("score", 50);
        user.setProperty("dateTime", date);

        datastore.put(user);

        resp.sendRedirect("/admin/userstored.jsp");
    }
}
